Named Instances for Haskell Type Classes
نویسندگان
چکیده
Although the functional programming language Haskell has a powerful type class system, users frequently run into situations where they would like to be able to define or adapt instances of type classes only after the remainder of a component has been produced. However, Haskell’s type class system essentially only allows late binding of type class constraints on free type variables, and not on uses of type class members at variable-free types. In the current paper we propose a language extension that enhances the late binding capabilities of Haskell type classes, and provides more flexible means for type class instantiation. The latter is achieved via named instances that do not participate in automatic context reduction, but can only be used for late binding. By combining this capability with the automatic aspects of the Haskell type class system, we arrive at an essentially conservative extension that greatly improves flexibility of programming using type classes and opens up new structuring principles for Haskell library design. We exemplify our extension through the sketch of some applications and show how our approach could be used to explain or subsume other language features as for example implicit parameters. We present a typed λ-calculus for our extension and provide a working prototype type checker on the basis of Mark Jones’ “Typing Haskell in Haskell”. 1 Email: [email protected] 2 Email: [email protected] 71
منابع مشابه
Type Class Instances for Type-Level Lambdas in Haskell
Haskell 2010 lacks flexibility in creating instances of type classes for type constructors with multiple type arguments. We would like to make the order of type arguments to a type constructor irrelevant to how type class instances can be specified. None of the currently available techniques in Haskell allows to do this in a satisfactory way. To flexibly create type-class instances we have adde...
متن کاملExplicitly Typed Exceptions for Haskell
We describe a monad for checked, explicitly typed exceptions, which provides as a simple Haskell library what for other languages is a native feature. Multi parameter type classes and overlapping instances are the only essential extensions to Haskell 98 required.
متن کاملObjects to Unify Type Classes and GADTs
We propose an Haskell-like language with the goal of unifying type classes and generalized algebraic datatypes (GADTs) into a single class construct. We treat classes as first-class types and we use objects (instead of type class instances and data constructors) to define the values of those classes. We recover the ability to define functions by pattern matching by using sealed classes. The res...
متن کاملA Type-Sensitive Preprocessor For Haskell
This paper presents a preprocessor which generates code from type de nitions. The preprocessor uses rules to describe the pattern of the produced code. In this way, instances may be derived for type classes. Utility functions for manipulating types, especially records, can also be produced. The set of rules is easily extendible; by adding derivation rules for new type classes, instance derivati...
متن کاملAnti-Unification with Type Classes
The anti-unification problem is that of finding the most specific pattern of two terms. While dual to the unification problem, anti-unification has rarely been considered at the level of types. In this paper, we present an algorithm to compute the least general type of two types in Haskell, using the logic programming power of type classes. That is, we define a type class for which the type cla...
متن کامل